package com.lill.org.web;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.org.constant.CompanyType;
import com.lill.org.constant.OrgConstant;
import com.lill.org.model.CompanyArchitectureModel;
import com.lill.org.model.CompanyInfoModel;
import com.lill.org.service.ICompanyArchitectureService;
import com.lill.org.service.ICompanyInfoService;
import com.lill.org.service.IDepartmentService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysUserEntity;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 公司架构Controller
 *
 * @author lill
 * @date 2019-04-24 15:25:17
 */
@RestController
@RequestMapping("org/architecture")
public class CompanyArchitectureController {
    @Autowired
    private ICompanyArchitectureService architectureService;
    @Autowired
    private ICompanyInfoService companyInfoService;
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("org:architecture:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<CompanyArchitectureModel> orgCompanyArchitectureList = architectureService.queryList(query);
        int total = architectureService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(orgCompanyArchitectureList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("org:architecture:info")
    public R info(@PathVariable("id") String id) {
        CompanyArchitectureModel orgCompanyArchitecture = architectureService.queryObject(id);

        return R.ok().put("orgCompanyArchitecture", orgCompanyArchitecture);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("orgcompanyarchitecture:save")
    public R save(@RequestBody CompanyArchitectureModel orgCompanyArchitecture) {
        int result=architectureService.save(orgCompanyArchitecture);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("org:architecture:update")
    public R update(@RequestBody CompanyArchitectureModel orgCompanyArchitecture,@CurrentUser SysUserEntity user) {
        int result=architectureService.update(orgCompanyArchitecture);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("orgcompanybranch:delete")
    public R delete(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	
        int result=architectureService.deleteLogicByMap(params);
		if(result>0){
        	return R.ok();
        }else{
        	return R.error(1, "有子部门或人员");
        }
    }
    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAllByemp")
    public R queryAllByemp(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
        if (SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
            if(null==params||params.size()<=0||!params.containsKey("parentList")
                    ||null==params.get("parentList")||"".equals(params.get("parentList").toString())){
                params.put("parentList", OrgConstant.DEFAULTCOMPANYID);
            }
        }else{
            params.put("parentList", curUser.getCompanyId());
        }

        boolean isCompany=true;
        CompanyInfoModel companyInfo=companyInfoService.queryObject(params.get("parentList").toString());
        if(null!=companyInfo&&null!=companyInfo.getType()&&CompanyType.GROUP.getVal()==companyInfo.getType()){
            isCompany=false;
        }
        String branchId=curUser.getBranchId();
        List<CompanyArchitectureModel> list = architectureService.queryByForList(params,branchId);
        return R.ok().put("list", list).put("isAdmin", SystemConstant.SUPER_ADMIN.equals(curUser.getId())).put("isCompany", isCompany);
    }
    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
    	if (SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
    		if(null==params||params.size()<=0||!params.containsKey("parentList")
    				||null==params.get("parentList")||"".equals(params.get("parentList").toString())){
    			params.put("parentList", OrgConstant.DEFAULTCOMPANYID);
    		}
    	}else{
                params.put("parentList", curUser.getCompanyId());
    	}
    	
    	boolean isCompany=true;
    	CompanyInfoModel companyInfo=companyInfoService.queryObject(params.get("parentList").toString());
    	if(null!=companyInfo&&null!=companyInfo.getType()&&CompanyType.GROUP.getVal()==companyInfo.getType()){
    		isCompany=false;
    	}
    	
        List<CompanyArchitectureModel> list = architectureService.queryList(params);
        return R.ok().put("list", list).put("isAdmin", SystemConstant.SUPER_ADMIN.equals(curUser.getId())).put("isCompany", isCompany);
    }
    
    @RequestMapping("/queryOwnAll")
    public R queryOwnAll(@CurrentUser SysUserEntity curUser) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("parentList", curUser.getCompanyId());
    	params.put("ownId", curUser.getCompanyId());
    	String levelData="company";
    	if(StringUtils.isNotEmpty(curUser.getBranchId())){
    		/**门店**/
    		if(!curUser.getBranchId().equals(curUser.getCompanyId())){
    			params.put("parentList", curUser.getBranchId());
    			params.put("ownId", curUser.getBranchId());
    			levelData="branch";
    		}
    	}else{
    		/**大区**/
    		if(StringUtils.isNotEmpty(curUser.getDepartId())){
    			String regionId=departmentService.queryFirstRegion(curUser.getDepartId());
    			if(StringUtils.isNotEmpty(regionId)){
    				params.put("parentList", regionId);
    				params.put("ownId", regionId);
    				levelData="region";
    			}
    		}
    	}
        return R.ok().put("list", architectureService.queryList(params)).put("levelData", levelData);
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryCompanyAll")
    public R queryCompanyAll(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
        if (SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
            if(null==params||params.size()<=0||!params.containsKey("parentList")
                    ||null==params.get("parentList")||"".equals(params.get("parentList").toString())){
                params.put("parentList", OrgConstant.DEFAULTCOMPANYID);
            }
        }else{
            params.put("parentList", curUser.getCompanyId());
        }

        boolean isCompany=true;
        CompanyInfoModel companyInfo=companyInfoService.queryObject(params.get("parentList").toString());
        if(null!=companyInfo&&null!=companyInfo.getType()&&CompanyType.GROUP.getVal()==companyInfo.getType()){
            isCompany=false;
        }

        List<CompanyArchitectureModel> list = architectureService.queryCompanyAll(params);

        return R.ok().put("list", list).put("isAdmin", SystemConstant.SUPER_ADMIN.equals(curUser.getId())).put("isCompany", isCompany);
    }


    /**
     * 销售统计查询所有机构
     */
    @RequestMapping("/customerQueryAll")
    public R customerQueryAll(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {

        boolean isAdmin = false;//  admin登录的
        boolean isCompany = false; // 集团账号登录的
        boolean isBranch = false; // 门店账号登录的
        if (SystemConstant.SUPER_ADMIN.equals(user.getId())) {
            isAdmin = true;
        } else if (user.getBranchId() == null || "".equals(user.getBranchId())) {
            isCompany = true;
        } else {
            isBranch = true;
        }
        if (SystemConstant.SUPER_ADMIN.equals(user.getId())) {
            if(null==params||params.size()<=0||!params.containsKey("parentList")
                    ||null==params.get("parentList")||"".equals(params.get("parentList").toString())){
                params.put("parentList", OrgConstant.DEFAULTCOMPANYID);
            }
        }else{
            if ( user.getBranchId() == null || "".equals(user.getBranchId())){
                params.put("parentList", user.getCompanyId());
            }else {
                params.put("branchId", user.getBranchId());
            }
        }

//        boolean isCompany=true;
//        CompanyInfoModel companyInfo=companyInfoService.queryObject(params.get("parentList").toString());
//        if(null!=companyInfo&&null!=companyInfo.getType()&&CompanyType.GROUP.getVal()==companyInfo.getType()){
//            isCompany=false;
//        }
//    	boolean isCompany=true;
//    	CompanyInfoModel companyInfo=companyInfoService.queryObject(params.get("parentList").toString());
//    	if(null!=companyInfo&&null!=companyInfo.getType()&&CompanyType.GROUP.getVal()==companyInfo.getType()){
//    		isCompany=false;
//    	}


        //修改禅道:1998 只查询公司旗下门店，不再查询子部门
        //List<CompanyArchitectureModel> list = architectureService.customerQueryAll(params);
        List<CompanyArchitectureModel> list = architectureService.customerQueryAllUpdate(params);
        return R.ok().put("list", list).put("isAdmin", isAdmin).put("isCompany", isCompany).put("isBranch", isBranch);
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryOrg")
    public R queryOrg(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
        if (SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
            if(null==params||params.size()<=0||!params.containsKey("parentList")
                    ||null==params.get("parentList")||"".equals(params.get("parentList").toString())){
                params.put("parentList", OrgConstant.DEFAULTCOMPANYID);
            }
        }else{
            params.put("parentList", curUser.getCompanyId());
        }
        List<CompanyArchitectureModel> list = architectureService.queryList(params);
        return R.ok().put("list", list);
    }
}
